package com.google.appengine.echogear.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import net.sf.jsr107cache.CacheException;
import net.sf.jsr107cache.CacheFactory;
import net.sf.jsr107cache.CacheManager;

public final class Cache {
	
	private static net.sf.jsr107cache.Cache cache;
	
	private static List<String> keyList;
	
	static {
		reset();
	}
	
	public static void reset() {
		keyList = new ArrayList<String>();
		try {
			CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
			cache = cacheFactory.createCache(Collections.emptyMap());			
		} catch (CacheException e) {
		}
	}
	
	public static void add(String name, Object instance) {
		cache.put(name, instance);
		keyList.add(name);
	}
	
	public static Collection<?> load() {
		List result = new ArrayList();
		for(String key : keyList) {
			result.add(cache.get(key));
		}
		return result;
	}

}
